/**
 * 籌碼兌換彈出框
 * 
 * @author luo
 * @email luo4521@online.net.tw
 * 2010-2-23 12:05
 */
package com.addcn.texas.view.components
{
	import flash.display.*;
	import flash.events.*;
	import flash.text.TextField;
	import fl.controls.NumericStepper;
	
	import com.addcn.common.event.ParamEvent;
	
	public class CarryBox extends winBase
	{		
		public static const BUY_EVENT : String = 'buyEvent';
		public static const CLOSE_EVENT : String = 'closeEvent';
		private var _parent : Object;
		private var _user_gold : int;
		
		function CarryBox (r_parent : Object) : void {	
			this._parent = r_parent;
			this.drag(this['bg']);					
			this["BuyBtn"].addEventListener(MouseEvent.CLICK, buyBtnClickHandler);		
			this["CloseBtn"].addEventListener(MouseEvent.CLICK, _closeBtnClick);						
		}		
		
		//初始化,[現有存款,最小買入,最大買入]
		public function initialize(r_data : Object) : void 
		{
			this._user_gold = r_data[0];
			this["UserGold"].text = String(r_data["gold"]);
			this["MinCarry"].text = String(r_data["minChip"]);
			this["MaxCarry"].text = String(r_data["maxChip"]);
			this["BuyGold"].stepSize = r_data["minChip"];
			this["BuyGold"].minimum = r_data["minChip"];
			this["BuyGold"].maximum = r_data["maxChip"];
			this["BuyGold"].value = ((r_data["maxChip"] - r_data[1])/2) >> 0;			
		}
		
		public function show() {			
			if (this._parent != null) {	
				this._parent.addChild(this);	
				this.x = (this._parent.stage.stageWidth -this.width)/ 2;
				this.y = (this._parent.stage.stageHeight - this.height) / 2;					
			}
		}

		private function _closeBtnClick(evt : Event) : void
		{
			this.remove();
			var event : Event = new Event(CarryBox.CLOSE_EVENT);
			this.dispatchEvent(event);	
		}
		
		public override function remove() :void {
			super.remove();
			if(this.parent != null){
				this.parent.removeChild(this);
			}
		}
		
		private function buyBtnClickHandler(evt : Event) : void {
			cheGold();					
			this["BuyBtn"].removeEventListener(MouseEvent.CLICK, buyBtnClickHandler);			
			this["CloseBtn"].removeEventListener(MouseEvent.CLICK, _closeBtnClick);	
			var event : Event = new ParamEvent(CarryBox.BUY_EVENT,{chip:parseInt(this["BuyGold"].value)});
			this.dispatchEvent(event);	
			this.remove();
		}
		
		private function cheGold() : void
		{
			this["BuyGold"].value = this["BuyGold"].textField.text ;
			var _val : int = parseInt(this["BuyGold"].textField.text);			
			if (_val >= this["BuyGold"].maximum)
			{
				_val = this["BuyGold"].maximum;
				this["BuyGold"].value = _val;	
			}else if(_val < this["BuyGold"].minimum){
				this["BuyGold"].value = this["BuyGold"].minimum;	
			}
			this["BuyGold"].textField.text = this["BuyGold"].value;
		}
		
		
		
		private function changeHandler() : void
		{			
						
		}
	}
	
}
